package LiKou;

public class ReverWater {
    public static void main(String[] args) {
        int[] height={0,1,0,2,1,0,1,3,2,1,2,1};
        System.out.println(trap(height));
    }
    public static int trap(int[] height){
        int result=0;
        int len=height.length;
        for(int i=1;i<len-1;i++){
            int maxRight=0;
            int maxLeft=0;
            int right=i+1;
            int left=i-1;
            while(right<len && left>=0){
                if(height[left]>maxLeft){
                    maxLeft=height[left];
                }
                left--;
                if(height[right]>maxRight){
                    maxRight=height[right];
                }
                right++;
            }
            while(right<len){
                if(height[right]>maxRight){
                    maxRight=height[right];

                }
                right++;
            }
            while (left>=0){
                if(height[left]>maxLeft){
                    maxLeft=height[left];
                }
                left--;
            }
            if((maxLeft>maxRight?maxRight:maxLeft)==0){
                continue;
            }
            if((maxLeft>maxRight?maxRight:maxLeft)<height[i]){
                continue;
            }
            result+=((maxLeft>maxRight?maxRight:maxLeft)-height[i]);
        }
        return result;
    }
    public static int trap2(int[] height){
        int result=0;
        return result;
    }
}
